﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;
using LinFen;

public partial class webUserControl_SearchCustomer : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Cookies aCookies = new Cookies();
            int flag = int.Parse(aCookies.getCookies("cFlag"));
            int isType = int.Parse(aCookies.getCookies("cisType"));
            int districtID = int.Parse(aCookies.getCookies("cDistrictID"));
            int cityID = int.Parse(aCookies.getCookies("cCityID"));
            
            if (flag == 1)
            {
                AddItems(ddlCityID, "sysCityName", cityID.ToString(), 0);
                //ddlCityID.Enabled = false;

                ddlDistrictID.Items.Clear();
                Query q = new Query("sysDistrictName");
                q.AddWhere("cityid", ddlCityID.SelectedValue);
                IDataReader dr = q.ExecuteReader();
                while (dr.Read())
                {
                    ListItem li = new ListItem();
                    li.Value = dr[0].ToString();
                    li.Text = dr[1].ToString();
                    ddlDistrictID.Items.Add(li);
                }
            }

            if (flag == 0)
            {
                AddItems(ddlCityID, "sysCityName", cityID.ToString(), 0);
                ddlCityID.Enabled = false;

                ddlDistrictID.Items.Clear();
                Query q = new Query("sysDistrictName");
                q.AddWhere("cityid", ddlCityID.SelectedValue);
                IDataReader dr = q.ExecuteReader();
                while (dr.Read())
                {
                    ListItem li = new ListItem();
                    li.Value = dr[0].ToString();
                    li.Text = dr[1].ToString();
                    if (districtID.ToString() == dr[0].ToString())
                    {
                        li.Selected = true;
                    }
                    ddlDistrictID.Items.Add(li);
                }
                ddlDistrictID.Enabled = false;
            }

           
        }

    }
    static void AddItems(DropDownList ddl, string tb, string id, int isType)
    {
        if (isType == 1)
        {
            for (int i = 0; i <= 10; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                if (id == i.ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }
        else
        {
            Query q = new Query(tb);
            IDataReader dr = q.ExecuteReader();
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                if (id == dr[0].ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }

    }
    protected void btnSub_Click(object sender, EventArgs e)
    {
        SqlQuery q = new SqlQuery();
        q.From("vw_customerInfo");
        //q.Where("id").IsGreaterThan(0);
        if(tbCustomerName.Text!="")
        {
            q.AndExpression("customername").Like("%"+tbCustomerName.Text + "%");
        }
        if (tbAddress.Text != "")
        {
            q.AndExpression("addre").Like("%" + tbAddress.Text + "%");
        }
        if (tbLinkMan.Text != "")
        {
            q.AndExpression("linkman").Like("%" + tbLinkMan.Text + "%");
        }
        if (tbTelPhone.Text != "")
        {
            q.AndExpression("telphone").Like("%" + tbTelPhone.Text + "%");
        }
        if (tbYeWu.Text != "")
        {
            q.AndExpression("yewu").Like("%" + tbYeWu.Text + "%");
        }
        q.AndExpression("cityID").IsEqualTo(ddlCityID.SelectedValue);
        q.AndExpression("districtID").IsEqualTo(ddlDistrictID.SelectedValue);
        string[] orderby=new string[1];
        orderby[0] = "id";
        q.OrderDesc(orderby);
        GridView1.DataSource = q.ExecuteReader();
        GridView1.DataBind();
    }
    protected void ddlCityID_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
